﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.Forms
{
    public partial class frm_BorrowAdd : DevExpress.XtraEditors.XtraForm
    {
        private ucBorrow parent;
        private int toolOrQTool;
        private DataTable dt = new DataTable();

        public frm_BorrowAdd(ucBorrow fBorrow)
        {
            InitializeComponent();
            parent = fBorrow;
            this.CancelButton = this.btTCancel;
        }

        private void rdTool_CheckedChanged(object sender, EventArgs e)
        {
            BindDataTool(ToolConstants.Configuration.ToolType.IsTool);
            toolOrQTool = ToolConstants.Configuration.ToolType.IsTool;
            // "0" : Tool
            // "1" : QTool
        }

        private void rdQTool_CheckedChanged(object sender, EventArgs e)
        {
            BindDataTool(ToolConstants.Configuration.ToolType.IsQTool);
            toolOrQTool = ToolConstants.Configuration.ToolType.IsQTool;
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            dtpTDate.Value = DateTime.Today;
            rdTool.Checked = true;
            BindDataStaff();
            BindDataMachine();
        }

        private void btTAdd_Click_1(object sender, EventArgs e)
        {
            AddTool();
        }

        private void btTCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbMachine_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbMachine.SelectedItem != cmbMachine.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                int number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);

                if (number == 0)
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.MachineNotBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.MachineBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue, number.ToString()), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        private void cmbToolID_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoCu;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                int instockAtpresent = Utilities.CheckInput(fieldName, storedName, where);

                if (instockAtpresent <= 0)
                {
                    MessageBox.Show(ToolConstants.Message.Borrow.ToolOutStock, ToolConstants.Form.MessageBox.Notice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmbToolID.SelectedItem = cmbToolID.Items[0];
                }
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.ToolInStock, cmbToolID.SelectedValue, instockAtpresent.ToString()), ToolConstants.Form.MessageBox.Notice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
            }
        }
        

        #region Private methods

        private bool GetDataTool(Borrow o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Borrow.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolID.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Borrow.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbStaff.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Borrow.StaffCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            if (Utilities.IsNullOrEmpty(cmbMachine.SelectedValue.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.Borrow.MachineCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            if (Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Borrow.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            
            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Borrow.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoCu;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                int instockAtpresent = Utilities.CheckInput(fieldName, storedName, where);

                if (instockAtpresent < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.WrongQuantity, cmbToolID.SelectedValue, instockAtpresent.ToString(), txtTQuantity.Text), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Text = "";
                    txtTQuantity.Focus();
                    return false;

                }

            }

            #endregion

            o.Date = dtpTDate.Value;
            o.ToolID = cmbToolID.SelectedValue.ToString();
            o.Quantity = int.Parse(txtTQuantity.Text);
            o.StaffID = cmbStaff.SelectedValue.ToString();
            o.MachineID = cmbMachine.SelectedValue.ToString();
            o.ToolOrQTool = toolOrQTool;
            o.Note = memoTNote.Text;

            return true;
        }

        private void BindDataTool(int type)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            helper.ValueObject.ToolOrQTool = type;
            dt = helper.GetAllByToolType();
            DataRow row = dt.NewRow();

            //DisplayToolName(type);
            cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
            cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolID.SelectedItem = cmbToolID.Items[0];
        }

        private void BindDataStaff()
        {
            StaffHelper helper = new StaffHelper(new Staff());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbStaff.DataSource = dt;
            cmbStaff.DisplayMember = ToolConstants.Entities.Staff.FieldName.FullName;
            cmbStaff.ValueMember = ToolConstants.Entities.Staff.FieldName.StaffID;
            row[ToolConstants.Entities.Staff.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Staff.FieldName.StaffID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbStaff.SelectedItem = cmbStaff.Items[0];
        }

        private void BindDataMachine()
        {
            MachineHelper helper = new MachineHelper(new Machine());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbMachine.DataSource = dt;
            cmbMachine.DisplayMember = ToolConstants.Entities.Machine.FieldName.FullName;
            cmbMachine.ValueMember = ToolConstants.Entities.Machine.FieldName.MachineID;
            row[ToolConstants.Entities.Machine.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Machine.FieldName.MachineID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMachine.SelectedItem = cmbMachine.Items[0];
        }

        private void AddTool()
        {
            BorrowHelper helper = new BorrowHelper(new Borrow());
            if (GetDataTool(helper.ValueObject))
            {

                Utilities.Timer timer = new Utilities.Timer();

                if (helper.Insert() > 0)
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.AddSuccess);

                    dtpTDate.Value = DateTime.Now;
                    rdTool.Checked = true;
                    cmbToolID.SelectedItem = cmbToolID.Items[0];
                    txtTQuantity.Text = "";
                    cmbStaff.SelectedItem = cmbStaff.Items[0];
                    cmbMachine.SelectedItem = cmbMachine.Items[0];
                    memoTNote.Text = "";
                    dtpTDate.Focus();
                }
                else
                {
                    timer.lb = lblStatus;
                    timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();

            }
        }

        #endregion


    }
}